using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using DAL;
using Utility;

public partial class Admin_NhanVienList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        if (Request.Url.ToString().Contains("dangnhap.aspx") == false && Session["tk"] == null)
            Response.Redirect("~/Admin/dangnhap.aspx");
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_id_nv.Text != "")
        {
            c.add(Condition.AND, tbNhanVien.tbNhanVienColumns .IDNhanVien, Condition.LIKE,
                txt_id_nv.Text);
        }
        if (txt_tnv.Text != "")
        {
            c.add(Condition.AND, tbNhanVien .tbNhanVienColumns .TenNV, Condition.LIKE, txt_tnv.Text);
        }
        int total = 0;
        tbNhanVien .tbNhanVienCollection  nv_Collection =
            tbNhanVienBUS .Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
           tbNhanVien.tbNhanVienColumns .IDNhanVien.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = nv_Collection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
            Pager.Visible = false;
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbNhanVien  lsp = tbNhanVienBUS .GetByID(id);
                tbNhanVienBUS . Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);

    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {

            tbNhanVien  nv_tbl = (tbNhanVien )e.Item.DataItem;
            HyperLink hyp_nv = (HyperLink)e.Item.FindControl("hyp_nv");
            HyperLink hyp_dc = (HyperLink)e.Item.FindControl("hyp_dc");
            HyperLink hyp_dt = (HyperLink)e.Item.FindControl("hyp_dt");
            HyperLink hyp_email = (HyperLink)e.Item.FindControl("hyp_email");
            Literal lit_idnv = (Literal)e.Item.FindControl("lit_idnv");

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");


            hyp_nv.Text = nv_tbl.TenNV;
            hyp_nv.NavigateUrl = "NhanVien.aspx?id=" + nv_tbl.IDNhanVien.ToString();
            hyp_dc.Text = nv_tbl.DiaChi;
            hyp_dt.Text = nv_tbl.DienThoai;
            hyp_email.Text = nv_tbl.Email;
            lit_idnv.Text = nv_tbl.IDNhanVien.ToString();
            hf_id.Value = nv_tbl.IDNhanVien.ToString();

            hpl_Edit.NavigateUrl = "NhanVienUpdate.aspx?id=" + nv_tbl.IDNhanVien.ToString();
        }
    }
}
